package cn.school.controller.newExam;

import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.newExam.entity.ExamPlan;
import cn.school.newExam.service.ExamPlanService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/examPlan")
public class ExamPlanController {

    @Resource
    private ExamPlanService examPlanService;

    @PostMapping("/queryPage")
    public CommonResult<CommonPage<ExamPlan>> queryPage(@RequestBody ExamPlan dto){
        IPage<ExamPlan> page = examPlanService.queryPage(dto);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @GetMapping("/getExamPlanById/{id}")
    public CommonResult<ExamPlan> getExamPlanById(@PathVariable String id){
        ExamPlan examPlan = examPlanService.getExamPlanById(id);
        return CommonResult.success(examPlan);
    }

    @PostMapping("/add")
    public CommonResult add(@RequestBody ExamPlan examPlan){
        return examPlanService.add(examPlan) > 0 ? CommonResult.success() : CommonResult.failed();
    }

    @PostMapping("/isOnly")
    public CommonResult isOnly(@RequestBody ExamPlan dto){
        ExamPlan examPlan = examPlanService.queryExamPlan(dto);
        return examPlan == null ? CommonResult.success() : CommonResult.failed("该考试计划已存在");
    }

    @PutMapping("/update")
    public CommonResult update(@RequestBody ExamPlan examPlan){
        return examPlanService.update(examPlan) > 0 ? CommonResult.success() : CommonResult.failed();
    }

    @DeleteMapping("/delete")
    public CommonResult delete(@RequestParam Integer[] ids){
        return examPlanService.delete(ids) == ids.length ? CommonResult.success() : CommonResult.failed();
    }
}
